// view/index/tousu/tousu.js
let app = getApp()
let yuming = app.globalData.yuming
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    heights: "",
    title: '被投诉',
    huan: ['本周', '本月', '本年'],
    currtab: 0,
    name: [],
  
    idx:0,
    status:1,
    // itenz: ['有异议', '已自行协商处理', '让监管局处理'],
    // itenz: [
    //   {
    //     cont: '有异议',
    //     img: yuming + 'static/applet/images/tousu/yiyi.png'
    //   },
    //   {
    //     cont: '已协商处理',
    //     img: yuming + 'static/applet/images/tousu/xieshang.png'
    //   },
    //   {
    //     cont: '监管介入',
    //     img: yuming + 'static/applet/images/tousu/shopjianguan.png'
    //   }
    // ],
    tab: [{
        id: 0,
        title: '未处理'
      },
     
      {
        id: 2,
        title: '处理中'
      },
      {
        id: 1,
        title: '已处理'
      },
    ],
    itindex: 0,
    page: 1,
    gongwu: false,
    isTi: true,
    isbei: false,
    jq_remark: '', //监管接入备注
    tijiao_id: '',
    start: '开始时间',
    endtime: '结束时间',
    begintime: '',
    endtimes: '',
    keywords: '',
    weichuliList:"",
    chuliListing:''
  },
  kgqux() {
    this.setData({
      isbei: false,
      jq_remark: ''
    })
  },
  bindjq_remark(e) {
    this.setData({
      jq_remark: e.detail.value
    })
  },
  zhe_isbei() {
    this.setData({
      isbei: false
    })
  },
  tab(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    if (index == 0) {
      that.setData({
        status: 1
      })
    }
    if (index == 1) {
      that.setData({
        status: 3
      })
    }
    if (index == 2) {
        that.setData({
          status: 2
        })
      }
    that.setData({
      idx: index,
      start: '开始时间',
      begintime: '',
      endtime: '结束时间',
      endtimes: '',
      keywords: ''
    })
    that.lie()
  },
  bindkaishi(e) {
    this.setData({
      start: e.detail.value,
      begintime: e.detail.value,
    })
    this.lie()
  },
  bindjieshu(e) {
    if (e.detail.value < this.data.begintime) {
      wx.showToast({
        title: '结束时间不能小于开始时间',
        icon: 'none'
      })
    } else {
      this.setData({
        endtime: e.detail.value,
        endtimes: e.detail.value,
      })
      this.lie()
    }
  },
  soud_dou_inp(e) {
    this.setData({
      keywords: e.detail.value
    })
    this.lie()
  },
  // 查看更多
  lookmore() {
    shuzong = this.data.name
    this.setData({
      page: (this.data.page) + 1
    })
    this.lie()
  },
  // 列表
  lie() {
    var that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Shop/shopSelectComplaint', {
      shopId: wx.getStorageSync('shopId'),
      keywords:that.data.keywords,
      status:that.data.status,
      begintime:that.data.begintime,
      endtime:that.data.endtimes,
      page: that.data.page
    }, 'POST').then(res => {
      wx.hideLoading();
      if (res.data.code == 200) {
        shu = res.data.data
        shu.forEach(item => {
          item.itindex = -1,
            item.isTi = true,
            item.itenz = [
              // {
              //   cont: '有异议',
              //   img: yuming + 'static/applet/images/tousu/yiyi.png'
              // },
              {
                cont: '协商处理',
                // img: yuming + 'static/applet/images/tousu/xieshang.png'
                img: yuming + 'static/applet/images/tousu/xieshang_xuan.png'
              },
              {
                cont: '监管介入',
                // img: yuming + 'static/applet/images/tousu/shopjianguan.png'
                img: yuming + 'static/applet/images/tousu/shopjianguan_xuan.png'
              }
            ]
        });
        shu.forEach(item => {
          item.itenz.forEach((items, index) => {
            if (item.complaintStatus == index + 1) {
              item.itindex = index;
              item.isTi = false;
            }
          })
        })
        console.log(shu);
        if (that.data.page != 1) {
          if (shu.length != 10) {
            wx.showToast({
              title: '没有更多数据了~',
              icon: "none"
            })
            that.setData({
              gongwu: true
            })
          }
        }
        that.setData({
          name: shuzong.concat(shu)
        })
      }
    })
  },
  lieAS() {
    var that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Shop/shopSelectComplaint', {
      shopId: wx.getStorageSync('shopId'),
      keywords:that.data.keywords,
      status:1,
      begintime:that.data.begintime,
      endtime:that.data.endtimes,
      page: that.data.page
    }, 'POST').then(res => {
     
      if (res.data.code == 200) {
       that.setData({
        weichuliList:res.data.data
       })
      }
    })
  },
  lieASING() {
    var that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Shop/shopSelectComplaint', {
      shopId: wx.getStorageSync('shopId'),
      keywords:that.data.keywords,
      status:3,
      begintime:that.data.begintime,
      endtime:that.data.endtimes,
      page: that.data.page
    }, 'POST').then(res => {
     
      if (res.data.code == 200) {
       that.setData({
        chuliListing:res.data.data
       })
      }
    })
  },
  //放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    var arr = []
    for (let i in list) {
      arr.push(list[i]); //属性
    }
    wx.previewImage({
      current: img,
      urls: arr
    })
  },
  queding(e) {
    var index = e.currentTarget.dataset.index
    var id = e.currentTarget.dataset.id
    this.setData({
      tijiao_id: id
    })
    var tel = e.currentTarget.dataset.tel;
    var that = this
    let msg = ''
    var dian_zuo = ''
    var dian_you = ''
    if (index == 0) {
      msg = '请选择协商方式'
      dian_zuo = '拨打'
      dian_you = '已协商'
    }
    // else if (index == 1) {
    //   msg = '已与投诉者协商处理'
    // }
    else if (index == 1) {
      msg = '是否反馈监管部门处理'
      dian_zuo = '否'
      dian_you = '是'
    }
    wx.showModal({
      title: msg,
      cancelText: dian_zuo,
      confirmText: dian_you,
      success: function (res) {
        if (index == 0) {
          if (res.confirm) {
            that.tijiao(id, 1)
          } else {
            that.Tel(tel)
          }
        }
        if (index == 1) {
          if (res.confirm) {
            console.log('是')
            that.setData({
              isbei: true
            })
            // that.tijiao(id,2)
          } else {

          }
        }
      }
    })
  },
  // tab切换
  qies(id, index) {
    this.data.name.forEach(item => {
      if (item.id == id) {
        item.itindex = index
        item.isTi = false
        if (index == 0) {
          item.itenz[index].img = yuming + 'static/applet/images/tousu/yiyi_xuan.png'
        } else if (index == 1) {
          item.itenz[index].img = yuming + 'static/applet/images/tousu/xieshang_xuan.png'
        } else if (index == 2) {
          item.itenz[index].img = yuming + 'static/applet/images/tousu/shopjianguan_xuan.png'
        }
        this.tijiao(id, index);
      }
    })
    console.log(this.data.name);
    this.setData({
      name: this.data.name,
    })
  },
  jgqueding() {
    if (this.data.jq_remark == '') {
      wx.showToast({
        title: '请输入备注',
        icon: 'none'
      })
    } else {
      this.setData({
        isbei: false
      })
      this.tijiao(this.data.tijiao_id, 2)
    }
  },
  tijiao(id, index) {
  //   complaintStatus
  // 0 商家未处理-用户与企业协商
  // 1 监管未处理-用户申请监管介入

  // 3 已协商待用户确定
  // 5 企业申请监管介入
  // 2 已协商用户已确定（已完成）
  // 4 监管已处理（已完成）
    console.log(id, index)
    let data = {};
    // 协商处理
    if (index == 1) {
      data = {
        id: id,
        complaintStatus: 3,
        user_handler: '',
      }
    }
    // 监管介入
    if (index == 2) {
      data = {
        id: id,
        complaintStatus: 5,
        user_handler: '',
        jg_remark:this.data.jq_remark
      }
    }
    // if (index == 0) {
    //   data = {
    //     id: id,
    //     complaintStatus: 0,
    //     user_handler: 2,
    //     remark: '有异议'
    //   }
    // } else if (index == 2) {
    //   data = {
    //     id: id,
    //     complaintStatus: 0,
    //     user_handler: 2,
    //     // remark: '协商无果，申请监管处理。'
    //     jq_remark: this.data.jq_remark //监管介入备注
    //   }
    // } else {
    //   data = {
    //     id: id,
    //     complaintStatus: index,
    //     user_handler: 1,
    //   }
    // }
    this.myRequest(yuming + 'Shop/shopSaveComplaint', data, 'POST').then(res => {
      console.log(res);
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        var that = this
        setTimeout(function () {
          that.lie()
          that.setData({
            jq_remark: '',
          })
        }, 1000)
      }
    })
  },
  Tel: function (tel) {
    var tel = tel
    wx.makePhoneCall({
      phoneNumber: tel,
      success: function () {
        console.log("拨号成功！")
      },
      fail: function () {
        console.log("拨号失败！")
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title
    })
    shuzong = [];
    shu = [];
    this.lie()
    this.lieAS()
    this.lieASING()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})